package com.cjm.mhl.view;

import com.cjm.mhl.domain.*;
import com.cjm.mhl.service.BillSerivce;
import com.cjm.mhl.service.DiningTableService;
import com.cjm.mhl.service.EmployeeService;
import com.cjm.mhl.service.MenuService;
import com.cjm.mhl.utils.Utility;
import jdk.jshell.execution.Util;

import java.util.List;

//菜单的打印
public class MHLview {
    private boolean loop=true;//控制是否退出菜单
    private String key;//用于接收输入信息
    private EmployeeService employeeService=new EmployeeService();
    private DiningTableService diningTableService=new DiningTableService();
    private MenuService menuService=new MenuService();
    private BillSerivce billSerivce=new BillSerivce();

    public static void main(String[] args) {
        new MHLview().mainMenu();
    }
    //完成结账
    public void payBill(){
        System.out.println("=========结账服务=========");
        System.out.print("请选择需要结账的餐桌号（-1退出）:");
        int i = Utility.readInt();
        if (i==-1){
            System.out.println("=========取消结账=========");
            return;
        }
    //验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTable(i);
        if (diningTable==null){
            System.out.println("=========结账餐桌不存在=========");
            return;
        }
        //验证是否有结账账单
        if (!billSerivce.hasPayBiningTableId(i)){
            System.out.println("=========没有账单结算=========");
            return;
        }
        System.out.print("请选择结账方式（回车表示退出）:");
        String PayMode = Utility.readString(20, "");
        if ("".equals( PayMode)){
            System.out.println("=========取消结账=========");
            return;
        }
        char c = Utility.readConfirmSelection();
        if (c=='Y'){
            if (billSerivce.payBill(i,PayMode)){
                System.out.println("=========结账成功=========");
            }else {
                System.out.println("=========结账失败=========");
            }
        }else {
            System.out.println("=========取消结账=========");
        }
    }


    //显示账单信息
    public void listBill(){
        List<MulitiTableBenn> list2 = billSerivce.list2();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名");
        for(MulitiTableBenn b:list2){
            System.out.println(b);
        }
        System.out.println("=========显示完毕=========");
    }


    //完成点餐
    public void orderMenu(){
        System.out.println("=========点餐服务=========");
        System.out.print("请输入点餐的桌号（-1退出）：");
        int orderDiningtableId = Utility.readInt();
        if(orderDiningtableId==-1){
            System.out.println("=========取消点餐=========");
            return;
        }
        System.out.print("请输入点餐的菜品号（-1退出）：");
        int orderMenuId = Utility.readInt();
        if(orderMenuId==-1){
            System.out.println("=========取消点餐=========");
            return;
        }
        System.out.print("请输入点餐的菜品量（-1退出）：");
        int orderEmployeeId = Utility.readInt();
        if(orderEmployeeId==-1){
            System.out.println("=========取消点餐=========");
            return;
        }
        //验证餐桌号
        DiningTable diningTable = diningTableService.getDiningTable(orderDiningtableId);
        if(diningTable==null){
            System.out.println("=========餐桌号有误=========");
        }
//        验证菜品号
        Menu menuById = menuService.getMenuById(orderMenuId);
        if(menuById==null){
            System.out.println("=========菜品号有误=========");
        }
        if (billSerivce.orderMenu(orderMenuId,orderEmployeeId,orderDiningtableId)){
            System.out.println("=========点餐成功=========");
        }else {
            System.out.println("=========点餐失败=========");
        }
    }


    //显示所有菜品
    public void listMenu(){
        List<Menu> list = menuService.list();
        System.out.println("\n菜品编号\t菜品名\t\t类别\t\t\t" +
                "价格");
        for(Menu menu:list){
            System.out.println(menu);
        }
        System.out.println("=========显示完毕=========");
    }


    //完成订座
    public void oderDiningTable() {
        System.out.println("=========预定餐桌=========");
        System.out.print("请选择预定餐桌编号（-1表示取消预定）：");
        int orderId = Utility.readInt(1);
        if (orderId==-1){
            System.out.println("=========成功取消预定=========");
            return;
        }
        char c = Utility.readConfirmSelection();
        if (c=='Y'){
            DiningTable diningTable = diningTableService.getDiningTable(orderId);
            if (diningTable==null){
                System.out.println("=========餐桌不存在=========");
                return;
            }
            if (!("空".equals(diningTable.getState()))){
                System.out.println("=========餐桌处于忙碌中=========");
                return;
            }
            System.out.print("预定人姓名：");
            String name = Utility.readString(50);
            System.out.print("预定人电话：");
            String phone = Utility.readString(50);
            if (diningTableService.orderDiningTable(orderId,name,phone)){
                System.out.println("=========[餐桌"+orderId+"]预定成功=========");
            }else {
                System.out.println("=========餐桌预定失败=========");
            }
        }else {
            System.out.println("=========成功取消预定=========");
        }

    }


    //显示所有餐桌状态
    public void listDiningTable() {
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("=========显示结束=========");
    }


//    主菜单方法
    public void mainMenu() {
        while (loop){
            System.out.println("=========满汉楼=========");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.print("请输入你的选择：");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("输入员工账号:");
                    String id = Utility.readString(50);
                    System.out.print("输入员工密码：");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdandPwd(id, pwd);
                    if (employee != null) {
                        System.out.println("=========登录成功["+employee.getName()+"]=========");
                        while (loop){
                            System.out.println("=========满汉楼二级菜单=========");
                            System.out.println("\t\t 1 显示餐桌状态");
                            System.out.println("\t\t 2 预定餐桌");
                            System.out.println("\t\t 3 显示所有菜品");
                            System.out.println("\t\t 4 点餐服务");
                            System.out.println("\t\t 5 查看账单");
                            System.out.println("\t\t 6 结账");
                            System.out.println("\t\t 9 退出满汉楼");
                            System.out.print("请输入你的选择：");
                            key=Utility.readString(1);
                            switch (key){
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    oderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop=false;
                                    break;
                                default:
                                    System.out.println("输入错误,请重新输入");
                            }

                        }
                    }else {
                        System.out.println("=========登录失败=========");
                    }
                    break;
                case "2":
                    loop=false;
                    break;
                default:
                    System.out.println("你的输入有误，请重新输入");
            }
        }
        System.out.println("退出了满汉楼系统");
    }
}
